package com.mod.db;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mod.test.testMain;



public class DBHelper {
	private static Log log = LogFactory.getLog(testMain.class);
	private QueryRunner qr; 
	private BeanHandler bh;
	private Connection conn;
	//同 _g_columnListHandler 同样是返回单一列时用到
//	private final static ScalarHandler g_scaleHandler = new ScalarHandler(){
//		@Override
//		public Object handle(ResultSet rs) throws SQLException {
//			Object obj = super.handle(rs);
//			if(obj instanceof BigInteger)
//				return ((BigInteger)obj).longValue();
//			return obj;
//		}		
//	};
//	
//	private final static List<Class<?>> PrimitiveClasses = new ArrayList<Class<?>>(){{
//		add(Long.class);
//		add(Integer.class);
//		add(String.class);
//		add(java.util.Date.class);
//		add(java.sql.Date.class);
//		add(java.sql.Timestamp.class);
//	}};
	
	public DBHelper(){
		qr = new QueryRunner();
		bh=new BeanHandler(null);
		conn=new DbcpBean().getConnAutoCommit();
		
	}
	
	@SuppressWarnings("unchecked")
	public  <T extends POJO> T read(Class<T> beanClass, String sql, Object...params) {
			try {
				//判断是否为原始类型，如果是就用_g_scaleHandler，否则根据beanClass参数生成一个BeanHandler
				return (T)qr.query(conn, sql,new BeanHandler(beanClass), params);
			} catch (SQLException e) {
				
				e.printStackTrace();
				return null;
			}
		
	}
	@SuppressWarnings("unchecked")
	public  <T extends POJO>  List readList(Class<T> beanClass, String sql, Object...params) {
			try {
				ResultSetHandler<List<T>> h = new BeanListHandler(beanClass);
				 return qr.query(conn, sql,h);
			} catch (SQLException e) {
				
				e.printStackTrace();
				return null;
			}
		
	}
	public <T extends POJO> String deleteHelper(T a){
		String sql="delete from "+a.getThis_table_name()+" where id='"+a.getId()+"'";
		System.out.println(sql);
		try {
			conn.setAutoCommit(false);
			int i=this.qr.update(conn, sql);
			conn.commit();
			System.out.println("删除成功，影响的行数："+i);
		} catch (SQLException e) {
			if(conn!=null){
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}		
			 System.out.println("删除"+a.getThis_table_name()+"表:id为"+a.getId()+"的记录失败。错误为："+e.getMessage());
			 e.printStackTrace();
		}finally{
			
		}
		
		return prama.succ;
	}
	/**
	 * 执行INSERT/UPDATE/DELETE语句
	 * @param sql
	 * @param params
	 * @return
	 */
	public  int update(String sql, Object...params) {
		try{
			System.out.println("sql execute");
			this.conn.setAutoCommit(true);
			//System.out.println(this.conn);
			 return this.qr.update(this.conn, sql, params);
			//this.conn.commit();
			//return 0;
		}catch(SQLException e){
			//throw e;
			e.printStackTrace();
			return 1;
		}
		
	}	
	//判断是否为原始类型
//	private final static boolean _IsPrimitive(Class<?> cls) {
//		return cls.isPrimitive() || PrimitiveClasses.contains(cls) ;
//	}
//	
//	public <T>BeanHandler getBh(Class<T> beanClass) {
//		if( _IsPrimitive(beanClass)){
//			//return this.g_scaleHandler;
//		}else{
//			bh=new BeanHandler(beanClass);
//			return bh;
//		}
//		return bh;
//		
//	}
	
}
